﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcEngine.Core.Repositories;

using MvcEngine.Core;
using MvcEngine.Core.Account;

namespace MvcEngine.LinqToSqlProvider
{
    public class PermissionRepository : Repository, IPermissionRepository
    {
        public IList<Permission> GetPermissions()
        {
            using (var contextManager = CreateContextManager(true))
            {
                return contextManager.Context.Permissions().ToList();
            }
        }

        public IList<ControlPermission> GetControlPermissions(int pageContentId)
        {
            using (var contextManager = CreateContextManager(true))
            {
                return contextManager.Context.ControlPermissions().Where(c => c.PageContentId == pageContentId).ToList();
            }
        }

        public ControlPermission GetControlPermission(int pageContentId, Guid permissionId, Guid roleId) 
        {
            using (var contextManager = CreateContextManager(true))
            {
                return contextManager.Context.ControlPermissions().SingleOrDefault(c => c.PageContentId == pageContentId&& c.PermissionId == permissionId && c.RoleId == roleId );
            }        
        }

        public void DeleteControlPermission(int pageContentId, Guid permissionId, Guid roleId)
        {
            var controlPermission = GetControlPermission(pageContentId, permissionId, roleId);
            if (controlPermission != null)
            {
                using (var contextManager = CreateContextManager(false))
                {
                    contextManager.Context.ControlPermissions().DeleteOnSubmit(controlPermission);
                    contextManager.Commit();
                }
            }
        }

        public void AddControlPermission(ControlPermission permission)
        {
            using (var contextManager = CreateContextManager(false))
            {
                contextManager.Context.ControlPermissions().InsertOnSubmit(permission);
                contextManager.Commit();
            }
        }

        public PagePermission GetPagePermission(Guid pageId, Guid permissionId, Guid roleId)
        {
            using (var contextManager = CreateContextManager(true))
            {
                return contextManager.Context.PagePermissions().SingleOrDefault(c => c.PageId == pageId && c.PermissionId == permissionId && c.RoleId == roleId);
            }
        }

        public IList<PagePermission> GetPagePermissions(Guid pageId)
        {
            using (var contextManager = CreateContextManager(true))
            {
                return contextManager.Context.PagePermissions().Where(p => p.PageId == pageId).ToList();
            }
        }

        public void DeletePagePermission(Guid pageId, Guid permissionId, Guid roleId)
        {
            var pagePermission = GetPagePermission(pageId, permissionId, roleId);
            if (pagePermission != null)
            {
                using (var contextManager = CreateContextManager(false))
                {
                    contextManager.Context.PagePermissions().DeleteOnSubmit(pagePermission);
                    contextManager.Commit();
                }
            }
        }

        public void DeletePagePermissions(Guid pageId)
        {
            var pagePermissions = GetPagePermissions(pageId);
            if (pagePermissions.Count > 0)
            {
                using (var contextManager = CreateContextManager(false))
                {
                    contextManager.Context.PagePermissions().DeleteAllOnSubmit(pagePermissions);
                    contextManager.Commit();
                }
            }
        }

        public void AddPagePermission(PagePermission permission)
        {
            using (var contextManager = CreateContextManager(false))
            {
                contextManager.Context.PagePermissions().InsertOnSubmit(permission);
                contextManager.Commit();
            }
        }
    }
}
